/* File:        	ResponseConturImpl.java
* Package:			org.esi.authz.pdp.response.contur
* Created:      	29-abr-2011
* Last Changed: 	0:10:47
* Author:       	Borja Urkizu borja.urquizu@tecnalia.com
*
*   
* History:
*  Revision 1.0.0.0  
* 
* Tecnalia Research and Innovation
*/
package com.tecnalia.authz.common.response.contur;

import java.util.Iterator;
import java.util.Set;

import org.jboss.security.xacml.factories.RequestResponseContextFactory;
import org.jboss.security.xacml.interfaces.RequestContext;
import org.jboss.security.xacml.interfaces.ResponseContext;
import org.jboss.security.xacml.interfaces.XACMLConstants;
import org.jboss.security.xacml.sunxacml.attr.AttributeValue;
import org.jboss.security.xacml.sunxacml.ctx.Attribute;
import org.jboss.security.xacml.sunxacml.ctx.ResponseCtx;
import org.jboss.security.xacml.sunxacml.ctx.Result;
import org.jboss.security.xacml.sunxacml.ctx.RequestCtx;

import com.tecnalia.authz.common.request.component.RequestAuthorizationImpl;
import com.tecnalia.authz.common.response.component.ResponseAuthorizationImpl;

// TODO: Auto-generated Javadoc
/**
 * The Class ResponseConturImpl.
 */
public class ResponseConturImpl implements IResponseContur{

	
	private ResponseContext simpleResponse = null;
	private ResponseContext[] listResponse = null;

	@Override
	public ResponseContext getResponse() {
		return simpleResponse;
	}


	@Override
	public void setResponse(ResponseContext response) {
		simpleResponse = response;
	}

	@Override
	public ResponseContext[] getListResponse() {
		return listResponse;
	}

	@Override
	public void setListResponse(ResponseContext[] listResponse) {
		this.listResponse = listResponse;
	}


	/* (non-Javadoc)
	 * @see org.esi.authz.pdp.response.contur.IResponseContur#flexibleResponseContext(org.jboss.security.xacml.interfaces.RequestContext[], org.jboss.security.xacml.interfaces.ResponseContext[])
	 */
	@Override
	public ResponseContext flexibleResponseContext(RequestContext[] reqList){
		
		ResponseContext responseAuthz = RequestResponseContextFactory.createResponseContext();
		String serviceList = "";
		for (int i = 0; i < listResponse.length; i++) {
			//ResponseCtx response = (ResponseCtx) res[i].get(XACMLConstants.RESPONSE_CTX);
			if( listResponse[i].getDecision() == 0 ){
				RequestCtx req = reqList[i].get("request_ctx");
			
				Set resources = req.getResource();
				for (Iterator<Attribute> iterator = resources.iterator(); iterator.hasNext();) {
					Attribute attr = iterator.next();
					AttributeValue service = attr.getValue();
					serviceList = serviceList + service.encode();
					//Avoid the last ,
					if( (i == listResponse.length - 1) && (!iterator.hasNext()) ){
					}else{
						serviceList = serviceList + ",";											
					}
						
				}
			}
		}		

		ResponseCtx responseAuthz1 = null;
		if( serviceList != ""){
			responseAuthz1 = new ResponseCtx(new Result(0, serviceList));			
		}else{
			responseAuthz1 = new ResponseCtx(new Result(1, ""));
		}

		
		responseAuthz.set(XACMLConstants.RESPONSE_CTX, responseAuthz1);
		return responseAuthz;

	}

	@Override
	public ResponseContext strictResponseContext() {
		// TODO Auto-generated method stub
		return null;
	}
    

}
